﻿using MyProject.Entities.Config;
using MyProject.Entities.User;
using MyProject.Services.MyNavigation;
using MyProject.ViewsModel.UserList;
using System.Windows.Controls;

namespace MyProject.Views.UserList
{
    /// <summary>
    /// Логика взаимодействия для UserList.xaml
    /// </summary>
    public partial class UserList : Page
    {
        private UserListContent context;
        public UserList(MyNavigationService navigationService)
        {
            this.context = new UserListContent(this, navigationService);
            this.DataContext = this.context;

            InitializeComponent();

            this.context.GenerateDataGridColumns(UserDataGrid);

            this.initMenuButton();
            this.context.getTable();
            this.BindingEvents();

        }

        private void initMenuButton() {

            System.Windows.Controls.Button menuButton = Mb_Menu;

            menuButton.Click += (object sender, System.Windows.RoutedEventArgs e) => {
                menuButton.ContextMenu.IsOpen = !menuButton.ContextMenu.IsOpen;
            };

            foreach (MenuItem menuItem in menuButton.ContextMenu.Items)
            {
                menuItem.Command = this.context?.InputCommand;
            }
        }

        public User? DataGridSelectedItem()
        {
            System.Windows.Controls.DataGrid dataGrid = UserDataGrid;
            return dataGrid.SelectedItem as User;
        }

        private void BindingEvents()
        {
            if (this.context != null)
            {
                UserDataGrid.CellEditEnding += this.context.CellEditEnding;
            }
        }

        public DataGrid getDataGrid()
        {
            return UserDataGrid;
        }
    }
}
